package org.bzjj.servlet.bean.ctrl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.bzjj.log.MyLog;
import org.bzjj.servlet.bean.CarInfo;
import org.bzjj.servlet.bean.CarReq;
import org.bzjj.sql.OracleCon;
import org.bzjj.tool.ChatMethod;
import org.bzjj.tool.TmriXmlUtil;

public class GetCarInfo extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();

		String admin = (String) session.getAttribute("role");
		String sql = "";
		boolean role = false;
		if (admin.equals("0")) {
			role = true;
			sql = "select t.* from VEH_IS_INOUT_REQ t where gxsj<(sysdate-1/24/60) and qqzt='0' and jkid!='01C73' order by gxsj desc";
		} else {
			role = false;
			sql = "select t.* from VEH_IS_INOUT_REQ t where gxsj<(sysdate-1/24/60) and qqzt='0' and jkid='01C40' order by gxsj desc";
		}

		ArrayList<CarInfo> cars = getCarList(sql, role);
		JSONArray array = JSONArray.fromObject(cars);

		JSONObject object = new JSONObject();
		object.element("total", cars.size());// 表中所有记录条数
		object.element("rows", array);

		ChatMethod.setResponse(response, object.toString());
	}

	/**
	 * 从数据库查询所有用户
	 * 
	 * @param query
	 *            查询语句
	 * @return
	 */
	public static ArrayList<CarInfo> getCarList(String query, boolean role) {
		ArrayList<CarInfo> rs = new ArrayList<CarInfo>();
		ResultSet result = null;
		CarInfo car = null;
		CarReq req = null;
		Connection connection = OracleCon.getDBonnect().getConnection();

		if (connection == null) {
			Logger logger = MyLog.getLog("GetCarInfo");
			logger.error("连接建立失败");
			return rs;
		}

		Statement stmt = null;
		try {
			stmt = connection.createStatement();
			result = stmt.executeQuery(query);

			while (result.next()) {
				if (role) {// 是否为管理员
					car = new CarInfo(result.getString("qqxlh"), "--", "--",
							result.getString("jkid"));
				} else {
					req = (CarReq) TmriXmlUtil.parseUTF8XML2Bean(
							result.getString("xmldoc"),
							"org.bzjj.servlet.bean.CarReq", "QueryCondition");

					car = new CarInfo(result.getString("qqxlh"), req.getHpzl(),
							req.getFzjg(), req.getHphm());
				}

				rs.add(car);
			}

		} catch (Exception e) {
			Logger logger = MyLog.getLog("GetCarInfo");
			logger.error("获取CarInfo错误", e);
		}

		OracleCon.getDBonnect().closeConnection(result, stmt, connection);

		return rs;

	}

}
